<?php

namespace app\controller;

class Home
{
    // 视图
    public string $view;

    // 查询
    public function query($seq, $req, $context)
    {
        if (!file_exists("download/")) {
            mkdir("download/", 0777, true);
        }
        $arr = getDirTree("download/");
        return ["code" => 0, "msg" => "获取成功", "data" => $arr];
    }

    // 创建
    public function create($seq, $req, $context)
    {
        $str = root_path . DIRECTORY_SEPARATOR . "download" . DIRECTORY_SEPARATOR;
        if (isset($req[2]) && $req[2] != "") {
            $str = $req[2] . DIRECTORY_SEPARATOR;
        }
        if ($req[1] == "file") {
            $file = str_replace("@", DIRECTORY_SEPARATOR,  $str . $req[0]) . ".md";
            if (file_exists($file)) {
                return [
                    "code" => 2,
                    "msg" => "文件已存在",
                    "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $file)
                ];
            }
            if ($handle = fopen($file, 'w')) {
                fwrite($handle, '# Hello, World!');
                fclose($handle);
                return [
                    "code" => 0,
                    "msg" => "操作成功",
                    "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $file)
                ];
            } else {
                return [
                    "code" => 1,
                    "msg" => "操作失败",
                    "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $file)
                ];
            }
        } else {
            $file = str_replace("@", DIRECTORY_SEPARATOR,  $str . $req[0]);
            if (file_exists($file)) {
                return [
                    "code" => 2,
                    "msg" => "文件已存在",
                    "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $file)
                ];
            }
            if (!file_exists($file)) {
                mkdir(str_replace("@", DIRECTORY_SEPARATOR,  $str . $req[0]), 0777, true);
                return [
                    "code" => 0,
                    "msg" => "操作成功",
                    "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $file)
                ];
            } else {
                return [
                    "code" => 1,
                    "msg" => "操作失败",
                    "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $file)
                ];
            }
        }
    }

    // 重命名
    public function renames($seq, $req, $context)
    {
        $file = str_replace("@", DIRECTORY_SEPARATOR, $req[0]);
        $name = basename($file);
        $extension = pathinfo($file, PATHINFO_EXTENSION);
        $new_file = rtrim($file, $name) . $req[1] . "." . $extension;
        if (rename($file, $new_file)) {
            return [
                "code" => 0,
                "msg" => "操作成功",
                "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $new_file)
            ];
        } else {
            return [
                "code" => 1,
                "msg" => "操作失败",
                "data" => str_replace(DIRECTORY_SEPARATOR, "@",  $new_file)
            ];
        }
    }

    // 获取文件内容
    public function getFileContent($seq, $req, $context)
    {
        $file =  file_get_contents(str_replace("@", DIRECTORY_SEPARATOR, $req[0]));
        return ["code" => 0, "msg" => "操作成功", "data" => $file];
    }

    // 打开目录
    public function opens($seq, $req, $context)
    {
        $folderPath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . "download";
        $command = "explorer.exe $folderPath";
        shell_exec($command);
    }

    // 删除
    public function deleteFile($seq, $req, $context)
    {
        $file  = str_replace("@", DIRECTORY_SEPARATOR, $req[0]);
        $res = delete_folder_or_file($file);
        if ($res) {
            return ["code" => 0, "msg" => "操作成功"];
        } else {
            return ["code" => 1, "msg" => "操作失败"];
        }
    }

    // 更新文件内容
    public function updateFile($seq, $req, $context)
    {
        $path = str_replace("@", DIRECTORY_SEPARATOR, $req[0]);
        $file = fopen($path, "w");
        fwrite($file, $req[1]);
        fclose($file);
    }
}
